home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu250.dms / pu250.adf / Graphics / VSprites / Example4.c < prev    next >
C/C++ Source or Header  |  1992-04-28  |  9KB  |  343 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Graphics                Amiga C Club       */
  7. /* Chapter: VSprites                    Tulevagen 22       */
  8. /* File:    Example4.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-28                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This example demonstrates how to use a VSprite  */
  21. /* on a display you have created yourself. We must */
  22. /* now use the low level functions MrgCop(), and   */
  23. /* LoadView(), instead of the more sophisticated   */
  24. /* functions MakeScreen(), RethinkDisplay().       */
  25.  
  26.  
  27. #include <intuition/intuition.h>
  28. #include <graphics/gfxbase.h>
  29. #include <graphics/gels.h>
  30.  
  31.  
  32. #define WIDTH  320 /* 320 pixels wide (low resolution)                 */
  33. #define HEIGHT 200 /* 200 lines high (non interlaced NTSC display)     */ 
  34. #define DEPTH    3 /* 3 BitPlanes should be used, gives eight colours. */
  35. #define COLOURS  8 /* 2^3 = 8                                          */
  36.  
  37.  
  38. struct IntuitionBase *IntuitionBase;
  39. struct GfxBase *GfxBase;
  40.  
  41.  
  42. struct View my_view;
  43. struct View *my_old_view;
  44. struct ViewPort my_view_port;
  45. struct RasInfo my_ras_info;
  46. struct BitMap my_bit_map;
  47. struct RastPort my_rast_port;
  48.  
  49.  
  50. UWORD my_color_table[] =
  51. {
  52.   0x000, /* Colour 0, Black       */
  53.   0x800, /* Colour 1, Red         */
  54.   0xF00, /* Colour 2, Light red   */
  55.   0x080, /* Colour 3, Green       */
  56.   0x0F0, /* Colour 4, Light green */
  57.   0x008, /* Colour 5, Blue        */
  58.   0x00F, /* Colour 6, Light Blue  */
  59.   0xFFF, /* Colour 7, White       */
  60. };
  61.  
  62.  
  63. /* Declare and initialize some sprite data: */
  64. UWORD chip vsprite_data[]=
  65. {
  66.   0x0180, 0x0000,
  67.   0x03C0, 0x0000,
  68.   0x07E0, 0x0000,
  69.   0x0FF0, 0x0000,
  70.   0x1FF8, 0x0000,
  71.   0x3FFC, 0x0000,
  72.   0x7FFE, 0x0000,
  73.   0x0000, 0xFFFF,
  74.   0x0000, 0xFFFF,
  75.   0x7FFE, 0x7FFE,
  76.   0x3FFC, 0x3FFC,
  77.   0x1FF8, 0x1FF8,
  78.   0x0FF0, 0x0FF0,
  79.   0x07E0, 0x07E0,
  80.   0x03C0, 0x03C0,
  81.   0x0180, 0x0180,
  82. };
  83.  
  84.  
  85.  
  86. /* Declare three VSprite structures. One will be used, */
  87. /* the other two are "dummies":                        */
  88. struct VSprite head, tail, vsprite;
  89.  
  90.  
  91. /* Decide the VSprite's colours:               */
  92. /*                         RGB     RGB     RGB */
  93. WORD colour_table[] = { 0x000F, 0x00F0, 0x0F00 };
  94.  
  95.  
  96. /* Declare a GelsInfo structure: */
  97. struct GelsInfo ginfo;
  98.  
  99.  
  100. /* This boolean variable will tell us if the VSprite is in */
  101. /* the list or not:                                        */
  102. BOOL vsprite_on = FALSE;
  103.  
  104.  
  105. void clean_up();
  106. void main();
  107.  
  108. void main()
  109. {
  110.   UWORD *pointer;
  111.   int loop;
  112.  
  113.   /* The GelsInfo structure needs the following arrays: */
  114.   WORD nextline[ 8 ];
  115.   WORD *lastcolor[ 8 ];
  116.  
  117.   /* Sprite position: */
  118.   WORD x = 1;
  119.   WORD y = 2;
  120.  
  121.   /* Direction of the sprite: */
  122.   WORD x_direction = 1;
  123.   WORD y_direction = 1;
  124.   
  125.  
  126.   /* Open the Intuition library: */
  127.   IntuitionBase = (struct IntuitionBase *)
  128.     OpenLibrary( "intuition.library", 0 );
  129.   if( !IntuitionBase )
  130.     clean_up( "Could NOT open the Intuition library!" );
  131.  
  132.   /* Open the Graphics library: */
  133.   GfxBase = (struct GfxBase *)
  134.     OpenLibrary( "graphics.library", 0 );
  135.   if( !GfxBase )
  136.     clean_up( "Could NOT open the Graphics library!" );
  137.  
  138.  
  139.   /* Save the current View, so we can restore it later: */
  140.   my_old_view = GfxBase->ActiView;
  141.  
  142.  
  143.   /* Prepare the View structure, and give it a pointer to */
  144.   /* the first ViewPort:                                  */
  145.   InitView( &my_view );
  146.   my_view.ViewPort = &my_view_port;
  147.  
  148.  
  149.   /* Prepare the ViewPort structure, and set some important values: */
  150.   InitVPort( &my_view_port );
  151.   my_view_port.DWidth = WIDTH;         /* Set the width.                  */
  152.   my_view_port.DHeight = HEIGHT;       /* Set the height.                 */
  153.   my_view_port.RasInfo = &my_ras_info; /* Give it a pointer to RasInfo.   */
  154.   my_view_port.Modes = SPRITES;        /* Using sprites.                  */
  155.  
  156.  
  157.   /* Get a colour map, link it to the ViewPort, and prepare it: */
  158.   my_view_port.ColorMap = (struct ColorMap *) GetColorMap( COLOURS );
  159.   if( my_view_port.ColorMap == NULL )
  160.     clean_up( "Could NOT get a ColorMap!" );
  161.  
  162.   /* Get a pointer to the colour map: */
  163.   pointer = (UWORD *) my_view_port.ColorMap->ColorTable;
  164.  
  165.   /* Set the colours: */
  166.   for( loop = 0; loop < COLOURS; loop++ )
  167.     *pointer++ = my_color_table[ loop ];
  168.  
  169.  
  170.   /* Prepare the BitMap: */
  171.   InitBitMap( &my_bit_map, DEPTH, WIDTH, HEIGHT );
  172.  
  173.   /* Allocate memory for the Raster: */ 
  174.   for( loop = 0; loop < DEPTH; loop++ )
  175.   {
  176.     my_bit_map.Planes[ loop ] = (PLANEPTR) AllocRaster( WIDTH, HEIGHT );
  177.     if( my_bit_map.Planes[ loop ] == NULL )
  178.       clean_up( "Could NOT allocate enough memory for the raster!" );
  179.  
  180.     /* Clear the display memory with help of the Blitter: */
  181.     BltClear( my_bit_map.Planes[ loop ], RASSIZE( WIDTH, HEIGHT ), 0 );
  182.   }
  183.  
  184.   
  185.   /* Prepare the RasInfo structure: */
  186.   my_ras_info.BitMap = &my_bit_map; /* Pointer to the BitMap structure.  */
  187.   my_ras_info.RxOffset = 0;         /* The top left corner of the Raster */
  188.   my_ras_info.RyOffset = 0;         /* should be at the top left corner  */
  189.                                     /* of the display.                   */
  190.   my_ras_info.Next = NULL;          /* Single playfield - only one       */
  191.                                     /* RasInfo structure is necessary.   */
  192.  
  193.   /* Create the display: */
  194.   MakeVPort( &my_view, &my_view_port );
  195.   MrgCop( &my_view );
  196.  
  197.  
  198.   /* Prepare the RastPort, and give it a pointer to the BitMap. */
  199.   InitRastPort( &my_rast_port );
  200.   my_rast_port.BitMap = &my_bit_map;
  201.   
  202.  
  203.   /* Show the new View: */
  204.   LoadView( &my_view );
  205.  
  206.  
  207.   /* Initialize the GelsInfo structure: */
  208.  
  209.   /* All sprites except the first two may be used to draw */
  210.   /* the VSprites: ( 11111100 = 0xFC )                    */
  211.   ginfo.sprRsrvd = 0xFC;
  212.   /* If we do not exclude the first two sprites, the mouse */
  213.   /* pointer's colours may be affected.                    */
  214.  
  215.  
  216.   /* Give the GelsInfo structure some memory: */
  217.   ginfo.nextLine = nextline;
  218.   ginfo.lastColor = lastcolor;
  219.  
  220.  
  221.   /* Give the Rastport a pointer to the GelsInfo structure: */
  222.   my_rast_port.GelsInfo = &ginfo;
  223.  
  224.   
  225.   /* Give the GelsInfo structure to the system: */
  226.   InitGels( &head, &tail, &ginfo );
  227.  
  228.  
  229.   /* Initialize the VSprite structure: */
  230.  
  231.   vsprite.Flags = VSPRITE; /* It is a VSprite.            */
  232.   vsprite.X = x;           /* X position.                 */
  233.   vsprite.Y = y;           /* Y position.                 */
  234.   vsprite.Height = 16;     /* 16 lines tall.              */
  235.   vsprite.Width = 2;       /* Two bytes (16 pixels) wide. */
  236.   vsprite.Depth = 2;       /* Two bitplanes, 4 colours.   */
  237.  
  238.  
  239.   /* Pointer to the sprite data: */
  240.   vsprite.ImageData = vsprite_data;
  241.  
  242.   /* Pointer to the colour table: */
  243.   vsprite.SprColors = colour_table;
  244.  
  245.  
  246.  
  247.   /* Add the VSprites to the VSprite list: */
  248.   AddVSprite( &vsprite, &my_rast_port );
  249.  
  250.   /* The VSprite is in the list. */
  251.   vsprite_on = TRUE;
  252.  
  253.  
  254.   for( loop = 0; loop < 1000; loop++ )
  255.   {
  256.     /* Change the x/y position: */
  257.     x += x_direction;
  258.     y += y_direction;
  259.  
  260.     /* Check that the sprite does not move outside the screen: */
  261.     if(x > 300)
  262.     {
  263.       x_direction = -1;
  264.       x = 300;
  265.     }
  266.     if(x < 1)
  267.     {
  268.       x_direction = 1;
  269.       x = 1;
  270.     }
  271.     if(y > 180)
  272.     {
  273.       y_direction = -1;
  274.       y = 180;
  275.     }
  276.     if(y < 10)
  277.     {
  278.       y_direction = 1;
  279.       y = 10;
  280.     }
  281.  
  282.     vsprite.X = x;
  283.     vsprite.Y = y;
  284.  
  285.     /* Sort the Gels list: */
  286.     SortGList( &my_rast_port );
  287.  
  288.     /* Draw the Gels list: */
  289.     DrawGList( &my_rast_port, &my_view_port );
  290.  
  291.     /* Adjust the copper list: */
  292.     MrgCop( &my_view );
  293.  
  294.     /* Show the new View: */
  295.     LoadView( &my_view );
  296.  
  297.     /* Move it smoothly: */
  298.     WaitTOF();
  299.   }
  300.  
  301.  
  302.   /* Restore the old View: */
  303.   LoadView( my_old_view );
  304.  
  305.  
  306.   /* Free all allocated resources and leave. */
  307.   clean_up( "THE END" );
  308. }
  309.  
  310.  
  311. /* Returns all allocated resources: */
  312. void clean_up( message )
  313. STRPTR message;
  314. {
  315.   int loop;
  316.  
  317.   /* Remove the VSprites: */
  318.   if( vsprite_on )
  319.     RemVSprite( &vsprite );
  320.  
  321.   /* Free automatically allocated display structures: */
  322.   FreeVPortCopLists( &my_view_port );
  323.   FreeCprList( my_view.LOFCprList );
  324.   
  325.   /* Deallocate the display memory, BitPlane for BitPlane: */
  326.   for( loop = 0; loop < DEPTH; loop++ )
  327.     if( my_bit_map.Planes[ loop ] )
  328.       FreeRaster( my_bit_map.Planes[ loop ], WIDTH, HEIGHT );
  329.  
  330.   /* Deallocate the ColorMap: */
  331.   if( my_view_port.ColorMap ) FreeColorMap( my_view_port.ColorMap );
  332.  
  333.   /* Close the Graphics library: */
  334.   if( GfxBase ) CloseLibrary( GfxBase );
  335.  
  336.   /* Close the Intuition library: */
  337.   if( IntuitionBase ) CloseLibrary( IntuitionBase );
  338.  
  339.   /* Print the message and leave: */
  340.   printf( "%s\n", message ); 
  341.   exit();
  342. }
  343.